﻿using Microsoft.Extensions.DependencyInjection;
using Model.Core.Entity.Dbcontexts;
using System.Collections.ObjectModel;

using Model.Core.Services;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.EntityFrameworkCore;

namespace RobotPratolSolution.ViewModels
{
    public class JobRecordViewModel :  ObservableObject
    {
        DbcontextBase _DbcontextBase { get; set; }
        LocalRobotInfo LocalRobotInfo { get; set; }
        public JobRecordViewModel()
        {

            _DbcontextBase = App.ServiceProvider.GetService<DbcontextBase>();

            LocalRobotInfo= App.ServiceProvider.GetService<LocalRobotInfo>();
        }

        public ObservableCollection<JobRecordItem> JobRecordItems { get; set; } = new ObservableCollection<JobRecordItem>();


        public void JobRecordGet(DateTime start, DateTime stop)
        {
            //var dataList =  _DbcontextBase.JobRecords.ToList();
            //  var dataList = _DbcontextBase.JobRecords.Where(a => a.RobotId.ToString() == App.RobotId).ToList();
            var dataList = _DbcontextBase.JobRecord.AsNoTracking().Where(a => a.RobotId == LocalRobotInfo.RobotId && a.StartTime >= start && a.StartTime < stop).ToList();

            JobRecordItems.Clear();
            for (int i = 0; i < dataList.Count; i++)
            {
                var item = dataList[i];
                JobRecordItems.Add(new JobRecordItem()
                {
                    JobId = item.JobId,
                    JobName = item.JobName,
                    RobotId = item.RobotId.ToString(),
                    StartTime = item.StartTime.ToString(),
                    StopTime = item.StopTime.ToString(),
                    StopType = item.StopType
                });
            }
        }

    }

    public class JobRecordItem : ObservableObject
    {
        //private Guid _Id;

        //public Guid Id
        //{
        //    get { return _Id; }
        //    set { Set(ref _Id, value); }
        //}

        private string _JobId;

        public string JobId
        {
            get { return _JobId; }
            set {  _JobId=value ;}
        }

        private string _StartTime;

        public string StartTime
        {
            get { return _StartTime; }
            set { _StartTime = value; }
        }

        private string _StopTime;

        public string StopTime
        {
            get { return _StopTime; }
            set { _StopTime = value; }
        }

        private string _JobName;

        public string JobName
        {
            get { return _JobName; }
            set { _JobName = value; }
        }
        private string _StopType;

        public string StopType
        {
            get { return _StopType; }
            set { _StopType = value; }
        }


        private string _RobotId;
        public string RobotId
        {
            get { return _RobotId; }
            set { _RobotId = value; }
        }

        private string _RobotName;
        public string RobotName
        {
            get { return _RobotName; }
            set { _RobotName = value; }
        }

    }
}
